﻿@using XRM.Security.WebUI.Helpers
@model XRM.Security.WebUI.Models.Department.DepartmentModel
@{
    ViewBag.Title = Model.IsEdit ? Model.Name : "Добавление подразделения";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@Html.Partial("ReturnLinkPartial", new Tuple<string, string>(Url.Action("Index"), "Вернуться к списку подразделений"))

<div>
    @using (Html.BeginForm("Edit", "Department", FormMethod.Post, new{@class="form-horizontal"}))
    {
        @Html.AntiForgeryToken()

        const string labelCss = "control-label required";
        const string inputCss = "form-control";

        @Html.HiddenFor(m => m.Id)

        <div class="form-group">
            @Html.LabelFor(m => m.Name, new { @class = @labelCss })
            <div class="col-sm-4">
                @Html.TextBoxFor(m => m.Name, new { @class = @inputCss })
                @Html.ValidationMessageFor(m => m.Name)
            </div>
        </div>
            
        <div class="form-group">
            @Html.LabelFor(m => m.AddressesList, new { @class = @labelCss })
            <div class="col-sm-4">
                <div data-bind="template: { name: 'department-template', foreach: DepartmentAddress }"></div>
                @if (!Model.ReadOnly)
                {
                <a href="#" id="add-address-link" data-bind="click: addAddress">Добавить адрес</a>
                }
            </div>
        </div>
        if( !Model.ReadOnly )   
        {
            @Html.Partial("_FormActionsPartial")
        }
    }
</div>

<script type="text/html" id="department-template">
    <div class="input-list-item" data-bind="css: { 'input-group': ($index() > 0) }">
        <input type="hidden" data-bind="attr: { name: 'AddressesList[' + $index() + '].Id' }, value: id" />
        <input type="hidden" data-bind="attr: { name: 'AddressesList[' + $index() + '].CanDelete' }, value: ((canDelete == 0)?false:true)" />
        <input type="text" class="form-control" maxlength="250" data-bind="attr: { name: 'AddressesList[' + $index() + '].Address' }, value: address, hasfocus: isFocused" />
        @if (!Model.ReadOnly)
        {
        <span class="input-group-btn" data-bind="visible: ($index() > 0)">
            <button title="Удалить адрес" class="btn btn-default" type="button" data-bind="click: $parent.removeAddress, disable: (canDelete == 0)">
                <i class="fa fa-times"></i>
            </button>
        </span>
        }
    </div>
</script>

@section scripts
{
    <script src="@Url.Content("~/Scripts/knockout-3.2.0.js")"></script>
    <script type="text/javascript">
        function DepartmentViewModel() {
            var self = this;

            self.DepartmentAddress = ko.observableArray([
                @{
                    if ((Model.AddressesList != null) && (Model.AddressesList.Count() > 0))
                    {
                        foreach (var item in Model.AddressesList)
                        {
                            <text>{ id: '@item.Id', address: '@Html.Raw(Utils.PrepareStringToJS(item.Address))', canDelete: '@(item.CanDelete?1:0)', isFocused: false },</text>
                        }
                    }
                }
            ]);

            self.addAddress = function () {
                self.DepartmentAddress.push({ id: "-1", address: "", canDelete: 1, isFocused: true });
            };

            self.removeAddress = function () {
                self.DepartmentAddress.remove(this);
            };
        }

        ko.applyBindings(new DepartmentViewModel());

        $(document).ready(function () {
            @if (Model.ReadOnly)
            {
                <text>ReadOnlyView();</text>
            }
        });
    </script>
}